package com.mask.im.server.netty.action.handler;

import com.mask.im.server.netty.enums.IMActionEnum;
import com.mask.im.server.netty.model.IMReceiveMessage;
import io.netty.channel.Channel;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;

/**
 * @className： MaskChatActionSocketHandler
 * @Description: 聊天消息操作(撤回)
 * @Author：jian
 * @Date：2025/8/29 11:35
 */

@Log4j2
@Component
public class IMChatActionHandler extends IMAbstractChatHandler {

    @Override
    public IMActionEnum getAction() {
        return IMActionEnum.MESSAGE_ACTION;
    }

    @Override
    public void handle(IMReceiveMessage receiveMessage, Channel channel) {
//        Long chatId = receiveMessage.getChatId();
//        if (null != chatId) {
////            IMFriendChatMessageDO imFriendChatMessage = imFriendChatMessageMapper.selectById(chatId);
//            IMFriendChatMessageDO imFriendChatMessage = new IMFriendChatMessageDO();
//            IMSendMessage websocketDTO = new IMSendMessage();
//            if (imFriendChatMessage.getIsRetracted()) {
//                websocketDTO.setCmd(WebSocketConstant.RETRACT_MESSAGE);
//                websocketDTO.setFlag(false);
//                websocketDTO.setMessage("消息好像已经被撤回了哦");
//                channel.writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(websocketDTO)));
//                return;
//            }
//            Long fromUserId = imFriendChatMessage.getSenderId();
//            Long userInfoId = MaskSecurityUtil.getCurrentUserId();
//            if (!userInfoId.equals(fromUserId)) {
//                websocketDTO.setCmd(WebSocketConstant.RETRACT_MESSAGE);
//                websocketDTO.setFlag(false);
//                websocketDTO.setMessage("撤回消息失败");
//                channel.writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(websocketDTO)));
//                return;
//            }
//            Date nowTime = new Date();
//            long millisDiff = Math.abs(nowTime.getTime() - imFriendChatMessage.getCreateTime().getTime());
//            // 取整（忽略秒）
//            long until = millisDiff / (60 * 1000);
//            if (Math.abs(until) >= 2) {
//                websocketDTO.setCmd(WebSocketConstant.RETRACT_MESSAGE);
//                websocketDTO.setFlag(false);
//                websocketDTO.setMessage("消息超过两分钟不能再撤回");
//                channel.writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(websocketDTO)));
//                return;
//            }
//            IMFriendChatMessageDO lastMaxIMFriendChatMessage = new IMFriendChatMessageDO();
////            IMFriendChatMessageDO lastMaxIMFriendChatMessage = imFriendChatMessageMapper.maxNoDeletedChatStorageId(userInfoId,
////                    imFriendChatMessage.getSenderId().equals(userInfoId) ? imFriendChatMessage.getReceiverId() : imFriendChatMessage.getSenderId());
//            imFriendChatMessage.setIsRetracted(true);
////            int i = imFriendChatMessageMapper.updateById(imFriendChatMessage);
//            int i = 0;
//
//            if (i == 0) {
//                websocketDTO.setCmd(WebSocketConstant.RETRACT_MESSAGE);
//                websocketDTO.setFlag(false);
//                websocketDTO.setMessage("撤回消息失败");
//                channel.writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(websocketDTO)));
//                return;
//            }
//
//            Channel toChannel = channelService.getUserIdChannel(String.valueOf(imFriendChatMessage.getReceiverId()));
//            if (lastMaxIMFriendChatMessage != null && lastMaxIMFriendChatMessage.getId().equals(chatId)) {
////                friendshipsService.update(null, new LambdaUpdateWrapper<Friendships>()
////                        .set(Friendships::getLatestNews, "你撤回了一条消息")
////                        .eq(Friendships::getOneUserId, userInfoId)
////                        .eq(Friendships::getTwoUserId,
////                                imFriendChatMessage.getSenderId().equals(userInfoId) ?
////                                        imFriendChatMessage.getReceiverId() : imFriendChatMessage.getSenderId())
////                );
//                if (lastMaxIMFriendChatMessage.getReceiverId() == null || lastMaxIMFriendChatMessage.getReceiverDeleted().equals(CommonConstant.FALSE)) {
////                    friendshipsService.update(null, new LambdaUpdateWrapper<Friendships>()
////                            .set(Friendships::getLatestNews, "对方撤回了一条消息")
////                            .eq(Friendships::getOneUserId, imFriendChatMessage.getSenderId().equals(userInfoId) ? imFriendChatMessage.getReceiverId() : imFriendChatMessage.getSenderId())
////                            .eq(Friendships::getTwoUserId, userInfoId));
//                }
//                IMSendMessage asideWebsocketDTO = new IMSendMessage();
//                asideWebsocketDTO.setCmd(30006);
//                channel.writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(asideWebsocketDTO)));
//                if (toChannel != null) {
//                    toChannel.writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(asideWebsocketDTO)));
//                }
//            }
//            websocketDTO.setCmd(WebSocketConstant.RETRACT_MESSAGE);
//            websocketDTO.setFlag(true);
//            websocketDTO.setMessage(imFriendChatMessage);
//            channel.writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(websocketDTO)));
//            if (toChannel != null) {
//                toChannel.writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(websocketDTO)));
//            }
//        }
    }
}
